package utilities;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ibm.ws.webservices.utils.BufferedWriter;

/**
 * Servlet implementation class DodajanjeNovicHandlerServlet
 */
@WebServlet("/DodajanjeNovicHandlerServlet")
public class DodajanjeNovicHandlerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DodajanjeNovicHandlerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String vrsta = request.getParameter("objavi");
		String naziv = request.getParameter("naziv");
		String vsebina = request.getParameter("vsebina");
		Calendar currentDate = Calendar.getInstance();
		Date datum_objave = new Date(currentDate.getTime().getTime());
		NoviceDAOImpl nov = new NoviceDAOImpl();
		nov.getConnection();
		
		if (vrsta.equals("Objavi")) {				// Goran 27.11.2011 - brez uporabe "Equals" nikoli ne vrne true
			int id_avtorja = 1;
			int id = -1;
			Entiteta novica = new Novica(id, id_avtorja, naziv, vsebina, datum_objave);
			nov.vstavi(novica);
		} else if (vrsta.equals("Uredi")) {			// Goran 27.11.2011 - brez uporabe "Equals" nikoli ne vrne true
			int id = Integer.parseInt(request.getParameter("id"));
			Novica novica = (Novica)nov.vrni(id);
			novica.setNaziv(naziv);
			novica.setVsebina(vsebina);
			novica.setDatum(datum_objave);
			nov.posodobi(novica.getId(), novica);
		}
		
		response.sendRedirect("PrikazNovicInitServlet");
	}

}
